package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.dto.NursingPlanDTO;
import com.zzyl.nursing.domain.vo.NursingPlanVO;
import com.zzyl.nursing.domain.vo.NursingProjectPlanVO;
import com.zzyl.nursing.domain.vo.NursingProjectVO;
import com.zzyl.nursing.service.INursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author jjw
 * @date 2025-09-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Long id)
    {
        NursingPlanVO nursingPlanVO = new NursingPlanVO();
        NursingPlan nursingPlan = getById(id);
        if(nursingPlan==null){
            throw new RuntimeException("数据不存在");
        }
        BeanUtils.copyProperties(nursingPlan,nursingPlanVO);
        Long planId = nursingPlan.getId();
        List<NursingProjectPlanVO> result = nursingProjectPlanService.selectNursingProjectPlan(planId);
        nursingPlanVO.setProjectPlans(result);
        return nursingPlanVO;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO nursingPlanDTO)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO,nursingPlan);
        save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
        if(projectPlans!=null&&projectPlans.size()>0){
            projectPlans.forEach(plan->plan.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO nursingPlanDTO)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO,nursingPlan);
        updateById(nursingPlan);
        nursingProjectPlanService.del(nursingPlan.getId());
        List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
        if(projectPlans!=null&&projectPlans.size()>0){
            projectPlans.forEach(project->{project.setPlanId(nursingPlan.getId());});
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids)
    {
        nursingProjectPlanService.removeByPlanIds(ids);
        removeByIds(Arrays.asList(ids));
        return 1;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> getAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectList(new QueryWrapper<NursingPlan>());
        return nursingPlans;
    }
}